﻿#include "Menu.h"

menu::menu(short* state, Style style = STYLE_VERTICAL)
{
	this->style = style;
	this->state = state;

	option = 0;
}


menu::~menu(void)
{
}


void menu::add(string name, short action)
{
	record tmp;
	tmp.name   = name;
	tmp.action = action;
	Menu.push_back(tmp);
}

void menu::show()
{
	for (int i = 0; i < Menu.size(); i++)
	{
		setTextColor(COLOR_YELLOW);

		if (style & STYLE_VERTICAL)
		{
			if (i > 0)
				cout << endl << L"    │" << endl;

			// Ornaments
			if (style & STYLE_ORNAMENTS)
			{
				if (i == 0)
					cout << L"    ┌";
				else if (i < 3)
					cout << L"    ├";
				else
					cout << L"    └";
			}

			// Active option
			if (option == i)
				setTextColor(COLOR_RED);

			cout << L"█";
			setTextColor(COLOR_WHITE);
			cout << " " << i+1 << " ";

			cout << Menu[i].name.c_str();
		}
		else if (style & STYLE_HORIZONTAL)
		{
			// Active option
			if (option == i)
				setTextColor(COLOR_RED);

			cout << "     ";
			cout << L"█";
			setTextColor(COLOR_WHITE);
			cout << " " << i+1 << " ";
			cout << Menu[i].name.c_str();
		}
	}
}

void menu::think()
{
	key:
	int KEY = getKey();

	switch(KEY)
	{
	case KEY_UP:
		if (option > 0 && style & STYLE_VERTICAL)
			option--;
		break;
	case KEY_DOWN:
		if (option < Menu.size()-1 && style & STYLE_VERTICAL)
			option++;
		break;
	case KEY_LEFT:
		if (option > 0 && style & STYLE_HORIZONTAL)
			option--;
		break;
	case KEY_RIGHT:
		if (option < Menu.size()-1 && style & STYLE_HORIZONTAL)
			option++;
		break;
	case KEY_ENTER:
		*state = Menu[option].action;
		break;
	default:
		goto key;
		break;
	}
}